﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace DwarfScanner
{
    class StringUtils
    {
        enum ReadingState 
        { 
            WhiteSpace, 
            Char 
        };

        public static string RemoveExtraSpaces(string str)
        {
            string result = "";

            ReadingState state = ReadingState.WhiteSpace; 

	        foreach (char ch in str)
	        {
                bool isWhiteSpace = (ch == ' ') || (ch == '\t') || (ch == '\v') || (ch == '\n') || (ch == '\r');
                if (isWhiteSpace)
		        {
                    if (state == ReadingState.Char)
			        {
                        result += ch;
			        }
                    state = ReadingState.WhiteSpace;
		        }
		        else
		        {
                    state = ReadingState.Char;
                    result += ch;
		        }		
	        }
	
            return result.Trim();
        }

        private static string[] substitutions;
        public static string AddSpecialSpaces(string str)
        {
            if (substitutions == null)
            {
                substitutions = new string[] 
                { 
                    "=",
                    "{", "}", "(", ")", "[", "]", 
                    ";", ",", ".", "?", ":",
                    "+", "-", "*", "/",                                
                    "!", "&&", "<", ">" 
                
                    //"||", "==",
                    //  "&", "|", "<<", ">>", "^",
                    // "%"
                };
            };

            foreach (string subst in substitutions)
            {
                str = str.Replace(subst, " " + subst + " ");
            }

            return str;
        }
        
        public static bool IsInditifier(string str)
        {
            string SIMPLE_CHARACTERS = "abcdefghijklmnopqrstuvwxyz";
            SIMPLE_CHARACTERS += SIMPLE_CHARACTERS.ToUpper();

            const string NUM_CHARS = "0123456789";
            string ID_CHARS = SIMPLE_CHARACTERS + NUM_CHARS + "_";

            return str.Length > 0 && 
                   SIMPLE_CHARACTERS.Contains(str[0]) && 
                   IsSubsetOf(str, ID_CHARS);
        }

        public static string PrepareString(string str)
        {
            return RemoveExtraSpaces(AddSpecialSpaces(str));
        }
        
        public static bool IsInteger(string str)
        {
            const string NUM_CHARS = "0123456789";

            return IsSubsetOf(str, NUM_CHARS);
        }

        private static bool IsSubsetOf(string needle, string haystack)
        {
            foreach (char ch in needle)
            {
                if (!haystack.Contains(ch))
                {
                    return false;
                }
            }

            return true;
        }
    }
}
